package com.supermap.guangzhou.weifang.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.supermap.guangzhou.weifang.R;
import com.supermap.guangzhou.weifang.utils.SharedPreferencesUtil;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FlashActivity extends AppCompatActivity {

    @BindView(R.id.iv_flash)
    ImageView ivFlash;
    @BindView(R.id.tv_info)
    TextView tvInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash);
        ButterKnife.bind(this);


        //启动右上角时间提示定时器
        startSecondInfo();

        //启动动画
        startAnimation();



    }

    int index=3;
    //启动右上角时间提示定时器
    public void startSecondInfo(){
        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(3);
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvInfo.setText(index+"s");
                    }
                });

                index--;
                if(index==0){
                    service.shutdown();
                }
                Log.e("index",index+"");

            }
        },0,1, TimeUnit.SECONDS);  //延迟0秒  每一秒执行一次


    }

    //启动动画
    public void startAnimation(){
        AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0.5f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            //动画结束 执行跳转
            @Override
            public void onAnimationEnd(Animation animation) {
                jumpGuidePage();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        ivFlash.startAnimation(alphaAnimation);
    }



    //右上角时间点击事件
    @OnClick(R.id.tv_info)
    public void onViewClicked() {
        jumpGuidePage();
    }


    //跳转Guide Activity
    public void jumpGuidePage(){

        String isFirstLogin = SharedPreferencesUtil.getString(this, "isFirstLogin", "");

        if("".equals(isFirstLogin)){
//            Intent intent = new Intent(this, GuideActivity.class);
            Intent intent = new Intent(this, LoginWebActivity.class);
            startActivity(intent);
            SharedPreferencesUtil.setString(this,"isFirstLogin","true");
        }else{
            Intent intent = new Intent(this, LoginWebActivity.class);
            startActivity(intent);
        }

        finish();

    }


    /**
     * 沉浸式
     * @param hasFocus
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}
